﻿package core.ui
{
    import core.enum.*;
    import core.image.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.ui.*;

    public class Cursor extends Bitmap
    {
        var ed:Tuyitu;
        var mox:int = 0;
        var moy:int = 0;
        public var freeze:Boolean;
        var p:Point;

        public function Cursor(param1:Tuyitu)
        {
            this.ed = param1;
            this.freeze = false;
            return;
        }// end function

        public function Trace(event:MouseEvent) : void
        {
            this.x = stage.mouseX + this.mox;
            this.y = stage.mouseY + this.moy;
            if (this.ed.canvas)
            {
                if (AppState.OnCanvas)
                {
                    this.ed.navigator.cx.text = String(int(this.ed.canvas.document.mouseX));
                    this.ed.navigator.cy.text = String(int((this.ed.canvas.document.mouseY + 1)));
                }
                else
                {
                    var _loc_2:String = "";
                    this.ed.navigator.cy.text = "";
                    this.ed.navigator.cx.text = _loc_2;
                }
            }
            return;
        }// end function

        public function Set(cx:ToolType, cx:Boolean = false) : void
        {
            if (!this.freeze || cx)
            {
                Mouse.hide();
                Mouse.cursor = "auto";
                this.bitmapData = null;
                this.blendMode = BlendMode.NORMAL;
                this.filters = [new GlowFilter(16777215, 0.75, 3, 3, 5, 1, false)];
                if (cx.attitude && cx.attitude.hasOwnProperty("locked") && cx.attitude.locked)
                {
                    this.mox = -8;
                    this.moy = -8;
                    this.bitmapData = new LockCursor();
                }
                else
                {
                    switch(cx)
                    {
                        case ToolType.None:
                        {
                            this.filters = null;
                            Mouse.cursor = "auto";
                            Mouse.show();
                            break;
                        }
                        case ToolType.Hand:
                        {
                            this.filters = null;
                            Mouse.cursor = "hand";
                            Mouse.show();
                            break;
                        }
                        case ToolType.Click:
                        {
                            this.filters = null;
                            Mouse.cursor = "button";
                            Mouse.show();
                            break;
                        }
                        case ToolType.Crop:
                        {
                            this.mox = -10;
                            this.moy = -9;
                            this.bitmapData = UI.Combine(new Crosshair(), new CropCursor(), 17, 16);
                            break;
                        }
                        case ToolType.Lock:
                        {
                            this.mox = -8;
                            this.moy = -8;
                            this.bitmapData = new LockCursor();
                            break;
                        }
                        case ToolType.Move:
                        {
                            switch(cx.attitude.state)
                            {
                                case "locked":
                                {
                                    this.bitmapData = UI.Combine(new MoveCursor(), new MiniLock(), 12, 13);
                                    this.mox = 0;
                                    this.moy = 1;
                                    break;
                                }
                                case "normal":
                                {
                                    this.bitmapData = UI.Combine(new MoveCursor(), new CrossIcon(), 12, 12);
                                    this.mox = 0;
                                    this.moy = 1;
                                    break;
                                }
                                case "cut":
                                {
                                    this.bitmapData = UI.Combine(new MoveCursor(), new SissorsIcon(), 12, 12);
                                    this.mox = 0;
                                    this.moy = 1;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                            break;
                        }
                        case ToolType.MovePoint:
                        {
                            this.mox = 0;
                            this.moy = 1;
                            this.bitmapData = new MoveCursor();
                            break;
                        }
                        case ToolType.MoveDrag:
                        {
                            this.mox = -1;
                            this.moy = 0;
                            this.bitmapData = UI.Combine(new MoveCursor(), new CrossIcon(), 12, 12);
                            break;
                        }
                        case ToolType.MoveCenter:
                        {
                            this.mox = -1;
                            this.moy = 0;
                            this.bitmapData = UI.Combine(new MoveCursor(), new CenterIcon(), 12, 12);
                            break;
                        }
                        case ToolType.Picker:
                        {
                            this.mox = -1;
                            this.moy = -17;
                            this.bitmapData = new PickerCursor();
                            break;
                        }
                        case ToolType.Fill:
                        {
                            this.mox = -18;
                            this.moy = -14;
                            this.bitmapData = new FillCursor();
                            break;
                        }
                        case ToolType.Rotate:
                        {
                            this.mox = -7;
                            this.moy = -7;
                            this.bitmapData = new RotateCursor();
                            break;
                        }
                        case ToolType.SkewHorizontal:
                        {
                            this.mox = -9;
                            this.moy = -5;
                            this.bitmapData = new SkewHorizontalCursor();
                            break;
                        }
                        case ToolType.SkewVertical:
                        {
                            this.mox = -5;
                            this.moy = -9;
                            this.bitmapData = new SkewVerticalCursor();
                            break;
                        }
                        case ToolType.ResizeHeight:
                        {
                            this.mox = -3;
                            this.moy = -7;
                            this.bitmapData = new ResizeHeightCursor();
                            break;
                        }
                        case ToolType.ResizeWidth:
                        {
                            this.mox = -7;
                            this.moy = -3;
                            this.bitmapData = new ResizeWidthCursor();
                            break;
                        }
                        case ToolType.ResizeHeightWidth:
                        {
                            this.mox = -7;
                            this.moy = -7;
                            this.bitmapData = new ResizeHeightWidthCursor();
                            break;
                        }
                        case ToolType.ResizeWidthHeight:
                        {
                            this.mox = -7;
                            this.moy = -7;
                            this.bitmapData = new ResizeWidthHeightCursor();
                            break;
                        }
                        case ToolType.Zoom:
                        {
                            this.mox = -8;
                            this.moy = -8;
                            switch(cx.attitude.state)
                            {
                                case "locked":
                                {
                                    this.bitmapData = new ZoomCursor();
                                    this.mox = -6;
                                    this.moy = -6;
                                    break;
                                }
                                case "minus":
                                {
                                    this.bitmapData = UI.Combine(new ZoomCursor(), new MinusIcon(), 3, 5);
                                    this.mox = -6;
                                    this.moy = -6;
                                    break;
                                }
                                case "plus":
                                {
                                    this.bitmapData = UI.Combine(new ZoomCursor(), new PlusIcon(), 3, 3);
                                    this.mox = -6;
                                    this.moy = -6;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                            break;
                        }
                        case ToolType.Marquee:
                        {
                            switch(cx.attitude.method)
                            {
                                case "normal":
                                {
                                    this.bitmapData = UI.Combine(new Crosshair(), new DottedSquareIcon(), 18, 18);
                                    this.mox = -10;
                                    this.moy = -9;
                                    break;
                                }
                                case "add":
                                {
                                    this.bitmapData = UI.Combine(UI.Combine(new Crosshair(), new DottedSquareIcon(), 18, 18), new PlusIcon(), 0, 20);
                                    this.mox = -10;
                                    this.moy = -9;
                                    break;
                                }
                                case "subtract":
                                {
                                    this.bitmapData = UI.Combine(UI.Combine(new Crosshair(), new DottedSquareIcon(), 18, 18), new MinusIcon(), 0, 22);
                                    this.mox = -10;
                                    this.moy = -9;
                                    break;
                                }
                                case "move":
                                {
                                    this.bitmapData = UI.Combine(new MoveCursor(), new DottedSquareIcon(), 12, 12);
                                    this.mox = 0;
                                    this.moy = 1;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                            break;
                        }
                        case ToolType.Wand:
                        {
                            switch(cx.attitude.method)
                            {
                                case "normal":
                                {
                                    this.bitmapData = new WandCursor();
                                    this.mox = -8;
                                    this.moy = -7;
                                    break;
                                }
                                case "add":
                                {
                                    this.bitmapData = UI.Combine(new WandCursor(), new PlusIcon(), 1, 16);
                                    this.mox = -8;
                                    this.moy = -7;
                                    break;
                                }
                                case "subtract":
                                {
                                    this.bitmapData = UI.Combine(new WandCursor(), new MinusIcon(), 1, 18);
                                    this.mox = -8;
                                    this.moy = -7;
                                    break;
                                }
                                case "move":
                                {
                                    this.bitmapData = UI.Combine(new MoveCursor(), new DottedSquareIcon(), 12, 12);
                                    this.mox = 0;
                                    this.moy = 1;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                            break;
                        }
                        case ToolType.Lasso:
                        {
                            switch(cx.attitude.method)
                            {
                                case "normal":
                                {
                                    this.bitmapData = new LassoCursor();
                                    this.mox = -2;
                                    this.moy = -18;
                                    break;
                                }
                                case "add":
                                {
                                    this.bitmapData = UI.Combine(new LassoCursor(), new PlusIcon(), 12, 15);
                                    this.mox = -2;
                                    this.moy = -18;
                                    break;
                                }
                                case "subtract":
                                {
                                    this.bitmapData = UI.Combine(new LassoCursor(), new MinusIcon(), 12, 17);
                                    this.mox = -2;
                                    this.moy = -18;
                                    break;
                                }
                                case "move":
                                {
                                    this.bitmapData = UI.Combine(new MoveCursor(), new DottedSquareIcon(), 12, 12);
                                    this.mox = 0;
                                    this.moy = 1;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                            break;
                        }
                        case ToolType.CloneMarker:
                        {
                            this.mox = -10;
                            this.moy = -9;
                            this.bitmapData = new ReferentCursor();
                            break;
                        }
                        case ToolType.Type:
                        {
                            switch(cx.attitude.state)
                            {
                                case "add":
                                {
                                    this.bitmapData = new TypeAddCursor();
                                    this.mox = -4;
                                    this.moy = -13;
                                    break;
                                }
                                case "edit":
                                {
                                    this.bitmapData = new TypeCursor();
                                    this.mox = -4;
                                    this.moy = -8;
                                    break;
                                }
                                case "move":
                                {
                                    this.bitmapData = UI.Combine(new MoveCursor(), new CrossIcon(), 12, 11);
                                    this.mox = 0;
                                    this.moy = 1;
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                            break;
                        }
                        default:
                        {
                            if (cx.attitude && cx.attitude.shape && this.ed.options.current.shape)
                            {
                                this.DrawOutline(this.ed.options.current.shape);
                                this.blendMode = BlendMode.DIFFERENCE;
                                this.filters = null;
                                this.mox = -int(this.width / 2) - 1;
                                this.moy = -int(this.height / 2);
                            }
                            else
                            {
                                this.bitmapData = new Crosshair();
                                this.mox = -10;
                                this.moy = -9;
                            }
                            break;
                        }
                    }
                }
                this.Trace(null);
            }
            return;
        }// end function

        private function DrawOutline(cx:Brush) : void
        {
            var _loc_2:OutlinePoint = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:* = undefined;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:* = undefined;
            var _loc_12:* = undefined;
            var _loc_13:BitmapData = null;
            this.bitmapData = new BitmapData(Math.ceil(cx.width * this.ed.canvas.zoomlevel), Math.ceil(cx.height * this.ed.canvas.zoomlevel), true, 16777215);
            if (this.ed.canvas.zoomlevel > 1)
            {
                _loc_5 = Math.ceil(this.ed.canvas.zoomlevel) - 1;
                _loc_6 = 0;
                _loc_7 = 0;
                _loc_8 = cx.outline.length;
                while (_loc_6 < _loc_8)
                {
                    
                    _loc_2 = cx.outline[_loc_6];
                    _loc_3 = Math.round(_loc_2.x * this.ed.canvas.zoomlevel);
                    _loc_4 = Math.round(_loc_2.y * this.ed.canvas.zoomlevel);
                    switch(_loc_2.location)
                    {
                        case 0:
                        {
                            this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                            _loc_7 = 1;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 1:
                        {
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 2:
                        {
                            _loc_3 = _loc_3 + _loc_5;
                            this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                            _loc_7 = 1;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 - _loc_7, _loc_4, 4289374890);
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 3:
                        {
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 4:
                        {
                            _loc_3 = _loc_3 + _loc_5;
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 5:
                        {
                            _loc_4 = _loc_4 + _loc_5;
                            this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                            _loc_7 = 1;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3, _loc_4 - _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 6:
                        {
                            _loc_4 = _loc_4 + _loc_5;
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 7:
                        {
                            _loc_3 = _loc_3 + _loc_5;
                            _loc_4 = _loc_4 + _loc_5;
                            this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                            _loc_7 = 1;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3, _loc_4 - _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 - _loc_7, _loc_4, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 8:
                        {
                            this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                            this.bitmapData.setPixel32(_loc_3 + _loc_5, _loc_4 + _loc_5, 4289374890);
                            _loc_7 = 1;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 + _loc_5, _loc_4 - _loc_7 + _loc_5, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 - _loc_7 + _loc_5, _loc_4 + _loc_5, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 9:
                        {
                            this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                            _loc_7 = 1;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4 + _loc_5, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 10:
                        {
                            _loc_3 = _loc_3 + _loc_5;
                            _loc_4 = _loc_4 + _loc_5;
                            this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                            _loc_7 = 1;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7 - _loc_5, _loc_4 - _loc_5, 4289374890);
                                this.bitmapData.setPixel32(_loc_3, _loc_4 - _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 - _loc_7, _loc_4, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 11:
                        {
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 + _loc_5, _loc_4 + _loc_7, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 12:
                        {
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4 + _loc_5, 4289374890);
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 + _loc_5, _loc_4 + _loc_7, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 13:
                        {
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3, _loc_4 + _loc_7, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 + _loc_5, _loc_4 + _loc_7, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        case 14:
                        {
                            _loc_7 = 0;
                            while (_loc_7 <= _loc_5)
                            {
                                
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4, 4289374890);
                                this.bitmapData.setPixel32(_loc_3 + _loc_7, _loc_4 + _loc_5, 4289374890);
                                _loc_7++;
                            }
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    _loc_6 = _loc_6 + 1;
                }
            }
            else
            {
                _loc_9 = 0;
                _loc_10 = cx.outline.length;
                while (_loc_9 < _loc_10)
                {
                    
                    _loc_2 = cx.outline[_loc_9];
                    _loc_3 = Math.round(_loc_2.x * this.ed.canvas.zoomlevel);
                    _loc_4 = Math.round(_loc_2.y * this.ed.canvas.zoomlevel);
                    this.bitmapData.setPixel32(_loc_3, _loc_4, 4289374890);
                    _loc_9++;
                }
            }
            if (this.bitmapData.width > 10 && this.bitmapData.height > 10)
            {
                _loc_11 = Math.ceil(cx.width * this.ed.canvas.zoomlevel * 0.5) - 1;
                _loc_12 = Math.ceil(cx.height * this.ed.canvas.zoomlevel * 0.5) - 1;
                this.bitmapData.setPixel32(_loc_11 - 2, _loc_12, 4289374890);
                this.bitmapData.setPixel32((_loc_11 - 1), _loc_12, 4289374890);
                this.bitmapData.setPixel32(_loc_11, _loc_12, 4289374890);
                this.bitmapData.setPixel32((_loc_11 + 1), _loc_12, 4289374890);
                this.bitmapData.setPixel32(_loc_11 + 2, _loc_12, 4289374890);
                this.bitmapData.setPixel32(_loc_11, _loc_12 - 2, 4289374890);
                this.bitmapData.setPixel32(_loc_11, (_loc_12 - 1), 4289374890);
                this.bitmapData.setPixel32(_loc_11, (_loc_12 + 1), 4289374890);
                this.bitmapData.setPixel32(_loc_11, _loc_12 + 2, 4289374890);
            }
            else
            {
                _loc_13 = this.bitmapData.clone();
                this.bitmapData = new BitmapData(11, 11, true, 16777215);
                this.bitmapData.copyPixels(_loc_13, _loc_13.rect, new Point(Math.round((11 - _loc_13.width) / 2), Math.round((11 - _loc_13.height) / 2)));
                _loc_13.dispose();
                _loc_13 = null;
                this.bitmapData.setPixel32(0, 5, 4289374890);
                this.bitmapData.setPixel32(1, 5, 4289374890);
                this.bitmapData.setPixel32(9, 5, 4289374890);
                this.bitmapData.setPixel32(10, 5, 4289374890);
                this.bitmapData.setPixel32(5, 0, 4289374890);
                this.bitmapData.setPixel32(5, 1, 4289374890);
                this.bitmapData.setPixel32(5, 9, 4289374890);
                this.bitmapData.setPixel32(5, 10, 4289374890);
            }
            return;
        }// end function

    }
}
